package cn.liuyi.weiyi;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.Toast;
import cn.yjr.fragment.PersonalCenterFragment;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.LayoutParams;
import com.actionbarsherlock.app.SherlockFragmentActivity;

import java.util.ArrayList;

public class HomeActivity extends SherlockFragmentActivity
{
  ActionBar actionBar;
  Context context;
  LayoutInflater inflater;
  boolean isExit;
  TabHost mTabHost;
  TabsAdapter mTabsAdapter;
  ViewPager mViewPager;
  View title_bar;

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.activity_home);
    this.context = this;
    this.isExit = false;
    this.inflater = getLayoutInflater();
    this.actionBar = getSupportActionBar();
    this.actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_bg));
    this.mTabHost = ((TabHost)findViewById(R.id.tabhost));
    this.mTabHost.setup();
    this.mViewPager = ((ViewPager)findViewById(R.id.pager));
    this.mTabsAdapter = new TabsAdapter(this, this.mTabHost, this.mViewPager);
    this.mTabsAdapter.addTab(this.mTabHost.newTabSpec("grzx").setIndicator("个人中心"), PersonalCenterFragment.class, null);
    this.mTabsAdapter.addTab(this.mTabHost.newTabSpec("zxs").setIndicator("咨询室"), PersonalCenterFragment.class, null);
    this.mTabsAdapter.addTab(this.mTabHost.newTabSpec("zcxm").setIndicator("众筹项目"), PersonalCenterFragment.class, null);
    this.mTabsAdapter.addTab(this.mTabHost.newTabSpec("blgl").setIndicator("病历管理"), PersonalCenterFragment.class, null);
    if (paramBundle == null)
      return;
    this.mTabHost.setCurrentTabByTag(paramBundle.getString("tab"));
  }
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event)
  {
    if (keyCode == KeyEvent.KEYCODE_BACK)
      if (!this.isExit)
      {
        this.isExit = true;
        Toast.makeText(this.context, "再点击一次退出", 0).show();
        new Handler().postDelayed(new Runnable()
        {
          public void run()
          {
            HomeActivity.this.isExit = false;
          }
        }
        , 2000L);
      }else{
    	  finish();
      }
    return super.onKeyUp(keyCode, event); 
  }

  protected void onSaveInstanceState(Bundle paramBundle)
  {
    super.onSaveInstanceState(paramBundle);
    paramBundle.putString("tab", this.mTabHost.getCurrentTabTag());
  }

  public static class TabsAdapter extends FragmentPagerAdapter
    implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener
  {
    private final Context mContext;
    private final TabHost mTabHost;
    private final ArrayList<TabInfo> mTabs = new ArrayList();
    private final ViewPager mViewPager;

    public TabsAdapter(FragmentActivity paramFragmentActivity, TabHost paramTabHost, ViewPager paramViewPager)
    {
      super(paramFragmentActivity.getSupportFragmentManager());
      this.mContext = paramFragmentActivity;
      this.mTabHost = paramTabHost;
      this.mViewPager = paramViewPager;
      this.mTabHost.setOnTabChangedListener(this);
      this.mViewPager.setAdapter(this);
      this.mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(TabHost.TabSpec paramTabSpec, Class<?> paramClass, Bundle paramBundle)
    {
      paramTabSpec.setContent(new DummyTabFactory(this.mContext));
      TabInfo localTabInfo = new TabInfo(paramTabSpec.getTag(), paramClass, paramBundle);
      this.mTabs.add(localTabInfo);
      this.mTabHost.addTab(paramTabSpec);
      notifyDataSetChanged();
    }

    public int getCount()
    {
      return this.mTabs.size();
    }

    public Fragment getItem(int paramInt)
    {
      TabInfo localTabInfo = (TabInfo)this.mTabs.get(paramInt);
      return Fragment.instantiate(this.mContext, localTabInfo.clss.getName(), localTabInfo.args);
    }

    public void onPageScrollStateChanged(int paramInt)
    {
    }

    public void onPageScrolled(int paramInt1, float paramFloat, int paramInt2)
    {
    }

    public void onPageSelected(int paramInt)
    {
      TabWidget localTabWidget = this.mTabHost.getTabWidget();
      int i = localTabWidget.getDescendantFocusability();
      localTabWidget.setDescendantFocusability(393216);
      this.mTabHost.setCurrentTab(paramInt);
      localTabWidget.setDescendantFocusability(i);
    }

    public void onTabChanged(String paramString)
    {
      int i = this.mTabHost.getCurrentTab();
      this.mViewPager.setCurrentItem(i);
    }

    static class DummyTabFactory
      implements TabHost.TabContentFactory
    {
      private final Context mContext;

      public DummyTabFactory(Context paramContext)
      {
        this.mContext = paramContext;
      }

      public View createTabContent(String paramString)
      {
        View localView = new View(this.mContext);
        localView.setMinimumWidth(0);
        localView.setMinimumHeight(0);
        return localView;
      }
    }

    static final class TabInfo
    {
      private final Bundle args;
      private final Class<?> clss;
      private final String tag;

      TabInfo(String paramString, Class<?> paramClass, Bundle paramBundle)
      {
        this.tag = paramString;
        this.clss = paramClass;
        this.args = paramBundle;
      }
    }
  }
}